
import { BaseViewCmpt } from '../../Frame/Script/Base/baseViewCmpt';
import { App } from '../app';
import { ResLoadHelper } from '../../Frame/Script/resLoadHelper';
import { ViewName } from '../../Frame/Script/const/viewNameConst';
import { OverItem } from '../item/OverItem';
const { ccclass, property } = cc._decorator;

const BG_HEIGHT = [600, 800];
const TITLE_OFFSET_Y = [285, 385];
const AT_BLUE_OFFSET_Y = [200, 300];
const CLOSE_OFFSET_Y = [250, 350];
const AGAIN_OFFSET_Y = [-370, -470];
const TITLE_NAMES = ["pingju", "shengli", "shibai"];

@ccclass
export class Model2OverViewManager extends BaseViewCmpt {


    private title: sp.Skeleton = null;
    private panelBg: cc.Node = null;
    private atBlue: cc.Node = null;
    private atRed: cc.Node = null;
    private btnClose: cc.Node = null;
    private btnAgain: cc.Node = null;


    protected onLoad(): void {
        super.onLoad();
        this.title = this.viewList.get("content/title").getComponent(sp.Skeleton);
        this.panelBg = this.viewList.get("content/img_bg");
        this.atBlue = this.viewList.get("content/img_at_blue");
        this.atRed = this.viewList.get("content/img_at_red");
        this.btnClose = this.viewList.get("content/btnClose");
        this.btnAgain = this.viewList.get("content/btnAgain");
    }

    start() {
        this.initViewContent();
    }

    private initViewContent() {
        let total_people = App.gameData.getPeopleNum();
        let settle_type = App.gameData.getSettleType();
        let idx = total_people / 2 - 1;
        this.panelBg.height = BG_HEIGHT[idx];
        this.title.node.setPosition(cc.v3(0, TITLE_OFFSET_Y[idx], 0));
        this.atBlue.setPosition(cc.v3(0, AT_BLUE_OFFSET_Y[idx], 0));
        this.btnClose.setPosition(cc.v3(265, CLOSE_OFFSET_Y[idx], 0));
        this.btnAgain.setPosition(cc.v3(0, AGAIN_OFFSET_Y[idx], 0));
        this.btnAgain.active = App.user.seatId !== 0;
        let offsetY: number[] = [];
        if (total_people === 2) {
            offsetY = [90, -180];
            this.addItem(offsetY);
        } else if (total_people === 4) {
            offsetY = [200, 80, -150, -270];
            this.addItem(offsetY);
        } else {
            console.error("人数错误");
        }
        let anima_name = `anim_${TITLE_NAMES[settle_type]}_${App.gameData.language}`;
        this.title.setAnimation(0, anima_name, false);
        this.atBlue.getChildByName("atCountLabel").getComponent(cc.Label).string = "" + App.gameData.blue_at_count;
        this.atRed.getChildByName("atCountLabel").getComponent(cc.Label).string = "" + App.gameData.red_at_count;
    }

    private async addItem(offsetY: number[]) {
        let seats = App.gameData.getGameSeats();
        let pre: cc.Prefab = await ResLoadHelper.loadAssetSync("prefabs/item/overItem", cc.Prefab);
        let item: cc.Node = null;
        for (let i = 0; i < 4; i++) {
            let seat_data = seats[i];
            let seatId = i + 1;
            if (seat_data.user) {
                let posY = offsetY.shift();
                let uid = seat_data.user.uid;
                let name = seat_data.user.name;
                let icon = seat_data.user.icon;
                item = cc.instantiate(pre);
                item.name = "p" + seatId;
                item.getComponent(OverItem).initItem(uid, name, icon, 0);
                item.parent = this.viewList.get("content");
                item.setPosition(cc.v3(0, posY, 0));
            }
        }
    }

    update(deltaTime: number) {

    }

    onClick_btnAgain() {
        this.onClick_closeBtn();
        App.view.openView(ViewName.Single.eHomeView);
    }

    onClick_btnClose() {
        this.onClick_closeBtn();
        App.view.openView(ViewName.Single.eHomeView);
    }
}


